package com.example.administrator.zyfypt_211dsh.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.zyfypt_211dsh.R;
import com.example.administrator.zyfypt_211dsh.adapter.VideoAdapter;
import com.example.administrator.zyfypt_211dsh.bean.SessionID;
import com.example.administrator.zyfypt_211dsh.bean.VideoBean;
import com.example.administrator.zyfypt_211dsh.iface.VideoListener;
import com.example.administrator.zyfypt_211dsh.model.VideoModel;

import java.util.List;


/**
 * Created by Neusoft on 2017/4/9.
 */

public class Fragment3_2 extends Fragment {
    private TextView tvinfo;
    private View view=null;
    private Context context;

    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;//显示布局效果
    private VideoAdapter videoAdapter;
    private List<VideoBean> list=null;

    VideoListener videoListener=new VideoListener() {
        @Override
        public void onResponse(List<VideoBean> beanlist) {
            list=beanlist;//得到网络数据
            videoAdapter.setList(list);//传给adapter
            videoAdapter.notifyDataSetChanged();//通知更新
        }


        @Override
        public void onFail(String msg) {
            Toast.makeText(context,msg, Toast.LENGTH_SHORT).show();
        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        context=getActivity();
       //动态加载Fragment3的布局文件
        view=inflater.inflate(R.layout.fragment3_2,container,false);
        //获取布局上的控件，注意通过view获取
        //tvinfo=(TextView)view.findViewById(R.id.textView8);
        //tvinfo.setText("视频");
        //注意Toast第一个参数上下文
        //Toast.makeText(context, "加载视频", Toast.LENGTH_SHORT).show();
        //返回动态生成的view
        initRecyclerView();

        //实例化ArticleModel,调用方法获取网络数据
        VideoModel videoModel=new VideoModel();
        videoModel.getResultList(
                "video",1,
                SessionID.sessionID,
                videoListener
        );
        return view;
    }
    private void initRecyclerView() {
        //获取RecyclerView,设置属性，获取数据源，绑定
        recyclerView=(RecyclerView)view.findViewById(R.id.recyclerview3_2);
        //创建默认的线性布局
        layoutManager=new LinearLayoutManager(context);
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //固定每个item高度，提高性能
        recyclerView.setHasFixedSize(true);
        //获取数据并创建Adapter
        //list =getData();
        //myAdapter=new MyAdapter(context);
        //myAdapter.setList(list);
        videoAdapter=new VideoAdapter(context);
        videoAdapter.setList(list);
        recyclerView.setAdapter(videoAdapter);
    }
}
